package com.mall.shopping.services;

import com.mall.shopping.IProductCateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Component //注册到容器
@Service(interfaceClass = IProductCateService.class)
public class ProductCateServiceImpl implements IProductCateService {
    @Autowired
    ItemCatMapper categoryMapper;

    @Autowired
    ProductCateConverter cateConverter;

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {

        Example example = new Example(ItemCat.class);

        example.setOrderByClause("sort_order" + " " + request.getSort());
        example.createCriteria().andEqualTo("status", 1);
        List<ItemCat> itemCats = categoryMapper.selectByExample(example);
        List<ProductCateDto> productCateDtos = cateConverter.items2Dto(itemCats);

        AllProductCateResponse allProductCateResponse = new AllProductCateResponse();
        allProductCateResponse.setProductCateDtoList(productCateDtos);
        allProductCateResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        allProductCateResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return allProductCateResponse;


    }
}
